﻿using SnowTech.SmartTms.Repository.Domain;
using SnowTech.SmartTms.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SnowTech.SmartTms.App.Interface;
using SnowTech.SmartTms.Repository.Interface;
using Infrastructure;
using Microsoft.EntityFrameworkCore;
using SnowTech.SmartTms.App.Request;
using StackExchange.Redis;
using SnowTech.SmartTms.App.TmsOilCostManager.Request;
using SnowTech.SmartTms.App.Response;
using Yitter.IdGenerator;

namespace SnowTech.SmartTms.App.BenzinkostenManager
{
    public class TmsOilCostApp : BaseStringApp<TmsOilCost, OpenAuthDBContext>
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private RevelanceManagerApp _revelanceApp;
        public TmsOilCostApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<TmsOilCost, OpenAuthDBContext> repository,
            RevelanceManagerApp app, IAuth auth) : base(unitWork, repository, auth)
        {
            _revelanceApp = app;
        }
        /// <summary>
        /// 加载当前登录用户可访问的全部油费
        /// </summary>
        public async Task<TableData> Load(QueryTmsOilCostReq request)
        {
            var loginContext = _auth.GetCurrentUser();//获取当前用户
            if (loginContext == null)
            {
                throw new CommonException("登录已过期", Define.INVALID_TOKEN);
            }

            var columnFields = loginContext.GetTableColumns("Resource");
            //if (columnFields == null || columnFields.Count == 0)
            //{
            //    throw new Exception("请在代码生成界面配置Resource表的字段属性");
            //}


            var result = new TableData();
            var resources = GetDataPrivilege("u");
            if (!string.IsNullOrEmpty(request.Operator))
            {
                resources = resources.Where(u => u.Operator.Contains(request.Operator));
            }

            if (!string.IsNullOrEmpty(request.PlateNumber))
            {
                resources = resources.Where(u => u.PlateNumber == request.PlateNumber);
            }
            result.data = resources.OrderBy(u => u.PlateNumber)
                .Skip((request.page - 1) * request.limit)
                .Take(request.limit).ToList();
            result.count = await resources.CountAsync();
            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="req"></param>
        public void Add(AddOrUpdateOilCostReq req)//req结尾的dto表示输入参数，必须对其进行验证
        {
            var obj = req.MapTo<TmsOilCost>();//用AutoMapper把输入的参数转换成实体对象
            obj.Id = YitIdHelper.NextId().ToString();//雪花Id
            obj.CreateTime = DateTime.Now;//创建时间
            obj.UpdateTime= DateTime.Now;
            obj.OilCostStatus = 0;//删除状态默认为0
            var user = _auth.GetCurrentUser().User;//获取当前登录用户对象
            obj.CreateId = user.Id; //创建者Id
            obj.UpdateId = user.Id;
            Repository.Add(obj); //执行仓储的Add方法
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj"></param>
        public void Update(AddOrUpdateOilCostReq obj)
        {
            var user = _auth.GetCurrentUser().User;
            UnitWork.Update<TmsOilCost>(u => u.Id == obj.Id, u => new TmsOilCost
            {
                Operator = obj.Operator,
                OilCost = obj.OilCost,
                PlateNumber = obj.PlateNumber,
                FuelQuantity = obj.FuelQuantity,
                Mileage = obj.Mileage,
                PaymentMethod = obj.PaymentMethod,
                Remarks = obj.Remarks,
                OilCostStatus=0,
                UpdateTime=DateTime.Now,
                UpdateId = user.Id
            });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public new void Delete(string[] ids)
        {
            UnitWork.ExecuteWithTransaction(() =>
            {
                UnitWork.Delete<TmsOilCost>(u => ids.Contains(u.Id));
                UnitWork.Save();
            });
        }
    }
}
